﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    /*  A message sent from one user to another.
     *  To facilitate this, a MessageRoster is used in the database.    */
    // !NOTE -- senderID and recipientID must be added
    // !NOTE -- this is because they do not have FK
    public class DBMessage
    {
        public static bool AddMessage(Message m)
        {
            try
            {
                m.MessageID = "tempvalue";
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Messages.AddObject(m);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //retrieve job
        public static Message RetrieveMessage(string retrieveID)
        {
            var messages = new Message();
            using (hireddbEntities db = new hireddbEntities())
            {       
                messages =
                    (from m in db.Messages
                    where m.MessageID == retrieveID
                    select m).SingleOrDefault(); ;
            }
            return messages;
        }
        public static List<Message> retrieveMessagesByRecipientId(string recipientId)
        {
            using (hireddbEntities db = new hireddbEntities()) {
                return
                    (from m in db.Messages
                     where m.RecipientID == recipientId
                     orderby m.Timestamp descending
                     select m).ToList<Message>();
            }
        }

        public static List<object> retrieveInbox(string userID)
        {
            List<Message> messageList = retrieveMessagesByRecipientId(userID);
            List<string> usernameList = new List<string>();

            foreach (Message m in messageList)
            {
                string senderID = m.SenderID;
                string accountType = DBAccount.RetrieveAccountTypeByID(senderID);

                using (hireddbEntities db = new hireddbEntities())
                {
                    switch (accountType)
                    {
                        case "CompanyAccount":
                            var ca =
                                (from a in db.CompanyAccounts
                                 where a.CAccountID == senderID
                                 select a).SingleOrDefault();
                            usernameList.Add(ca.Username);
                            break;
                        case "JobseekerAccount":
                            var jsa =
                                (from a in db.JobseekerAccounts
                                 where a.JAccountID == senderID
                                 select a).SingleOrDefault();
                            usernameList.Add(jsa.Username);
                            break;
                    }
                }
            }

            
                return
                    (from m in messageList
                     from u in usernameList
                     select new { MessageID = m.MessageID,
                         Username = u,
                         Title = m.Title,
                         Contents = m.Contents,
                         Timestamp = m.Timestamp
                     }).Distinct().ToList<object>();
        }

        //update job
        public static bool UpdateMessage(Message m)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Message messageToUpdate = RetrieveMessage(m.MessageID);
                    db.Attach(messageToUpdate);
                    messageToUpdate.Contents = m.Contents;
                    messageToUpdate.Timestamp = m.Timestamp;
                    messageToUpdate.Title = m.Title;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //delete job
        public static bool DeleteMessage(Message m)
        {
            try
            {
                Message messageToDelete = RetrieveMessage(m.MessageID);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(messageToDelete);
                    db.DeleteObject(messageToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool DeleteMessagebyId(string id)
        {
            try
            {
                Message messageToDelete = RetrieveMessage(id);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(messageToDelete);
                    db.DeleteObject(messageToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}